<template>
	<div class="components-container">
        <div class="header-form">
            <div class="layui-inline">
              <label class="layui-form-label" >统计条件</label>
              <div class="layui-input-inline">
                  <el-cascader :options="options" :show-all-levels="true" filterable v-model="selectData.options" change-on-select></el-cascader>
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label" >报表类型</label>
              <div class="layui-input-inline">
                <el-select v-model="date" placeholder="请选择" @change="selectChange">
                  <el-option
                    v-for="item in dataOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </div>
            </div>
            <div class="layui-inline">
                <label>处理日期从</label>
                <el-date-picker v-date v-model="selectData.ChddateGreaterAndEquals" type="date" placeholder="输入日期"/>
                <!-- <el-time-select v-model="selectData.ChdtimeGreaterAndEquals" :picker-options="{start: '06:00',step: '00:30',end: '18:00'}" placeholder="输入时间" style="width:110px;"/> -->
                 到 <el-date-picker v-date v-model="selectData.ChddateLessAndEquals" type="date" placeholder="输入日期"/>
                <!-- <el-time-select v-model="selectData.ChdtimeLessAndEquals" :picker-options="{start: '06:00',step: '00:30',end: '18:00'}" placeholder="输入时间" style="width:110px;"/> -->
            </div>
            <!-- <div class="layui-inline" style="margin-left:20px;margin-right:20px;">
              <div class="layui-input-inline">
                <el-checkbox v-model="ChdstatusEquals" label="已挂账"></el-checkbox>
              </div>
            </div> -->
            <el-button type="primary" :disabled="downloadLoading" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
            <el-button :disabled="this.$store.state.isloading || downloadLoading"  @click="btnRepeat">重置</el-button>
        </div>
        <div>
          <ul class="toolList">
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
              <el-button type="primary" :disabled="$store.state.isloading || downloadLoading" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
            <li>
            <el-button type="primary" :disabled="this.$store.state.isloading" @click="exportExcel">导出Excel</el-button>
            </li>
          </ul>
        </div>
        <printmodal ref="printPdf"></printmodal>
        <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow" :hiddenPageOrder="false" :height="height" :hiddenColumn="hiddenColumnInfo"></dataTable>
        <h2 style="text-align:center;">{{message}}</h2>
        <!-- 详情 -->
        <sweet-modal ref="dialogInfo" class="dialog" :sweetModalWidth="sweetModalWidth" :sweetModalMaxWidth="sweetModalMaxWidth">
          <h3>{{ title }}</h3>
          <br/>
          <dataTable :headList="headListInfo" :dataList="dataListInfo" :pageOrder="pageOrderInfo"  @pageChange="pageChangeInfo" :hiddenColumn="hiddenColumn"></dataTable>
        </sweet-modal>
	</div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.btn {
  text-align: center;
}
</style>
<script>
import dataTable from "@/components/Table/DataTable";
import printmodal from "@/components/Pdf/Print";
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import sweetModal from "@/components/sweetModal";

const EXPORT = "打印";
const INFO = "详情";

export default {
  name: "saleStatistics",
  mixins: [manage],
  data() {
    return {
      selectData: {
        ChaidEquals: null,
        ChddateGreaterAndEquals: null,
        ChddateLessAndEquals: null,
        options: [],
        CypidEquals:null
      },
      downloadLoading:false,
      OutDataExcel:[],
      ChdstatusEquals: {},
      chargetype: [],
      title: null,
      order: null,
      message: null,
      height: "620",
      hiddenColumnInfo: true,
      //价格历史
      dataListInfo: [],
      headListInfo: [],
      //分页和排序
      pageOrderInfo: {
        orderType: null,
        orderSort: null,
        totalNum: null,
        pageSize: 10,
        currentPage: 1
      },
      sweetModalWidth: 80,
      sweetModalMaxWidth: 1000,
      hiddenColumn: false,
      chaTypes: [],
      chaTypeList: [],
      options: [
        {
          value: "A",
          label: "项目类别",
          children: []
        },
        {
          value: "B",
          label: "项目名称",
          children: []
        }
      ],
      date: "day",
      dataOptions: [
        {
          value: "day",
          label: "日报",
        },
        {
          value: "mounth",
          label: "月报",
        },
        {
          value: "year",
          label: "年报",
        }
      ],
    };
  },
  methods: {
    exportExcel() {
      var header
      var startTime
      if(this.selectData.ChddateGreaterAndEquals){
        startTime = this.selectData.ChddateGreaterAndEquals.toLocaleDateString();
      }else{
        startTime = '空';
      }
      
      if(this.selectData.ChddateLessAndEquals != null){
        var endTime = this.selectData.ChddateLessAndEquals.toLocaleDateString();
        header = '火化业务报表' + '(' + startTime + '--' + endTime + ')';
      }else{
        header = '火化业务报表' + '(' + startTime + '--' + '至今' + ')';
      }
       if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.OutDataExcel, header);
        this.downloadLoading = false;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      this.dataRepeat();
      switch (this.order) {
        case INFO:
          this.title = "火化业务报表详情";
          this.dataInfo();
          this.$refs.dialogInfo.open();
          break;
        case EXPORT:
          let Data = JSON.parse(JSON.stringify(this.selectData));
          if (Data.options[0] === "A") this.$refs.printPdf.report(PRINT.PDFCEMBUSSRETBYCYP,this.getDataC());
          else this.$refs.printPdf.report(PRINT.PDFCEMBUSSRET,this.getDataC());
          break;
        default:
      }
    },
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetCemBussRetColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    //获取收费项目
    getChaType() {
      let data = { CYPTYPE: "B" };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Checkout.QueryChdByCyp,
        res => {
          let cdhTypeData = res.data.content;
          for (var i = 0; i < cdhTypeData.length; i++) {
            this.chaTypeList.push({
              value: cdhTypeData[i].CYPID,
              label: cdhTypeData[i].CYPNAME
            });
            for (var j = 0; j < cdhTypeData[i].list_Cha.length; j++) {
              this.chaTypes.push({
                value: cdhTypeData[i].list_Cha[j].CHAID,
                label: cdhTypeData[i].list_Cha[j].CHANAME
              });
            }
          }
          this.options[0].children = this.chaTypeList;
          this.options[1].children = this.chaTypes;
        },
        res => {
          this.$message.error(res.data.msg);
        }
      );
    },
    getDataC(){
      let Data = JSON.parse(JSON.stringify(this.selectData));
      // if (this.ChdstatusEquals)
      //   Data.ChdstatusEquals = 'D';
      Data.ChddateGreaterAndEquals = formatDate(
        Data.ChddateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.ChddateLessAndEquals = formatDate(
        Data.ChddateLessAndEquals,
        "yyyyMMdd"
      );
      //Data.ChdtimeGreaterAndEquals= formatDate(Data.ChdtimeGreaterAndEquals,"HHmm");
      //Data.ChdtimeLessAndEquals= formatDate(Data.ChdtimeLessAndEquals,"HHmm");
      if (Data.options[1] !== null && Data.options[1] !== undefined){
        if(Data.options[0] === "A"){
          Data.CypidEquals = Data.options[1];
        }else{
          Data.ChaidEquals = Data.options[1];
        }
      }
      let data = {
        PageIndex: 1,
        PageSize: 200,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      return data;
    },
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      // if (this.ChdstatusEquals)
      //   Data.ChdstatusEquals = 'D';
      Data.ChddateGreaterAndEquals = formatDate(
        Data.ChddateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.ChddateLessAndEquals = formatDate(
        Data.ChddateLessAndEquals,
        "yyyyMMdd"
      );
      //Data.ChdtimeGreaterAndEquals= formatDate(Data.ChdtimeGreaterAndEquals,"HHmm");
      //Data.ChdtimeLessAndEquals= formatDate(Data.ChdtimeLessAndEquals,"HHmm");
      if (Data.options[1] !== null && Data.options[1] !== undefined){
        if(Data.options[0] === "A"){
          Data.CypidEquals = Data.options[1];
        }else{
          Data.ChaidEquals = Data.options[1];
        }
      }
      let data = {
        PageIndex: 1,
        PageSize: 200,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      if (Data.options[0] === "A") {
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Report.QueryCemBussRetByCyp,
          res => {
            this.hiddenColumnInfo = false;
            this.dataList = res.data.content.OutData;
            this.OutDataExcel = res.data.content.OutDataExcel;
            this.message = res.data.content.Message;
            this.pageOrder.totalNum = res.data.content.RecordCount;
          }
        );
      } else {
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Report.QueryCemBussRet,
          res => {
            this.hiddenColumnInfo = true;
            this.dataList = res.data.content.OutData;
            this.OutDataExcel = res.data.content.OutDataExcel;
            this.message = res.data.content.Message;
            this.pageOrder.totalNum = res.data.content.RecordCount;
          }
        );
      }
    },
    //收费项目详情
    dataInfo() {
      var Data = {
        ChaidEquals: this.currentRow.CHLCHAID
      };
      let data = {
        PageIndex: this.pageOrderInfo.currentPage,
        PageSize: this.pageOrderInfo.pageSize,
        OrderBy: this.pageOrderInfo.orderType,
        OrderByType: this.pageOrderInfo.orderSort,
        Data: Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Report.QuerySaleStatisticsDetail,
        res => {
          this.dataListInfo = res.data.content.OutData;
          this.pageOrderInfo.totalNum = res.data.content.RecordCount;
        }
      );
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {
        ChddateGreaterAndEquals: null,
      };
      this.selectData.ChddateGreaterAndEquals = new Date();
      this.selectData.ChddateLessAndEquals = new Date();
      this.date = 'day'
      //this.ChdstatusEquals = true;
      this.selectData.options = ["A"];
      this.getData();
    },
    //分页和排序
    pageChangeInfo(value) {
      this.pageOrderInfo = value;
      this.dataInfo();
    },
    btnShow(btn){
      switch (btn) {
          case EXPORT:
            return true;
            break;
          default: return false;
        }
    },
    //数据重置
    dataRepeat() {},
    selectChange() {
      var nowMonth = new Date().getMonth();
      var nowYear = new Date().getYear();
      nowYear += (nowYear < 2000) ? 1900 : 0; 
      if(this.date === 'day'){
        this.selectData.ChddateGreaterAndEquals = new Date()
        console.log(this.selectData.ChddateGreaterAndEquals)
      } else if (this.date === 'mounth'){
        this.selectData.ChddateGreaterAndEquals = new Date(nowYear, nowMonth, 1)
        console.log(this.selectData.ChddateGreaterAndEquals)
      } else if (this.date === 'year'){
        this.selectData.ChddateGreaterAndEquals = new Date(nowYear, 0, 1)
        console.log(this.selectData.ChddateGreaterAndEquals)
      }
      // console.log(this.selectData.ChddateGreaterAndEquals)
      // console.log(this.selectData.ChddateLessAndEquals)
    }
  },
  mounted() {},
  components: {
    dataTable,
    sweetModal,
    printmodal
  },
  //当前选中行
  selectRow(row) {
    this.currentRow = row;
    this.$emit("selectRow", row, "");
  },
  created() {
    //this.ChdstatusEquals = true;
    this.selectData.ChddateGreaterAndEquals = new Date();
    this.selectData.ChddateLessAndEquals = new Date();
    this.selectData.options = ["A"];
    this.getColumns();
    this.getData();
    this.getChaType();
    this.headListInfo = [
      {
        ColumnAlign: "center",
        ColumnKey: "CHLMONEY",
        ColumnName: "单价",
        ColumnOrder: "CHLMONEY"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLNUM",
        ColumnName: "数量",
        ColumnOrder: "CHLNUM"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLMOUNT",
        ColumnName: "应收金额",
        ColumnOrder: "CHLMOUNT"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLREDUCTION",
        ColumnName: "减免金额",
        ColumnOrder: "CHLREDUCTION"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLDISCOUNT",
        ColumnName: "优惠金额",
        ColumnOrder: "CHLDISCOUNT"
      },
      {
        ColumnAlign: "center",
        ColumnKey: "CHLRECEIVED",
        ColumnName: "实收金额",
        ColumnOrder: "CHLRECEIVED"
      }
    ];
  }
};
</script>